<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Session;

//提货控制器

class UpGoods extends AdminBase
{

    public $statusName = [
        "0" => "已提交",
        "1" => "已确认",
        "2" => "已支付",
        "5" => "确认支付"
    ];


    public function index(){

        if( request()->isAjax() ){
            $map = input("param.");
            if( empty($map["status"]) ){
                $where = " status >= 0 ";
            }else{
                $where = " status = ".$map["status"];
            }

            if( !empty($map["start_time"]) ){

                $where .= " AND add_time >= ".strtotime($map["start_time"]);
            }

            if( !empty($map["end_time"]) ){
                if( empty($map["start_time"]) ){
                    $time = time() - (60*60*24*7);
                    $where .= " AND add_time >= ".$time;
                }
                $where .= " AND add_time <= ".strtotime($map["end_time"]);
            }

            if( !empty($map["order_sn"]) ){

                $where = " order_sn = '".$map["order_sn"]."'";
            }

            
            $count = Db::name("up_goods")->where($where)->count();
            

            $limitForm = ( $map["page"] -1 )* $map["limit"];
            $list = Db::name("up_goods")
                ->where($where)
                ->order("id desc")
                ->limit($limitForm,$map["limit"])
                ->select();
            $data = [];

            foreach($list as $k=>$v){
                $data[$k]["id"] = $v["id"];
                $data[$k]["order_sn"] = $v["order_sn"];
                $data[$k]["user_id"] = $v["user_id"];
                $data[$k]["goods_name"] = $v["goods_name"];
                $data[$k]["goods_price"] = $v["goods_price"];
                $data[$k]["num"] = $v["num"];
                $data[$k]["amount"] = $v["amount"];
                $data[$k]["add_time"] = date('y-m-d H:i:s', $v["add_time"]);
                //$data[$k]["statusName"] = $this->statusName[$v["status"]];
                $data[$k]["statusVal"] = $v["status"];
            }
            //print_r($v["status"]);exit;
            $retArr = [
                "code"=>0,
                "msg" => "",
                "count" => $count,
                "data" => $data
            ];
            echo json_encode($retArr);
            exit();
        }else{

            $map = input('param.');
            $url = '/admin/up_goods/index';
            $Arr = [];
            if( empty($map["status"]) ){
                $url.="?status=0";
                $Arr["status"] = '0';
            }else{
                $url.="?status=".$map["status"];
                $Arr["status"] = $map["status"];
            }


            if( !empty($map["start_time"]) ){
                $url.="&start_time=".$map["start_time"];
                $Arr["start_time"] = $map["start_time"];
            }
            if( !empty($map["end_time"]) ){
                $url.="&end_time=".$map["end_time"];
                $Arr["end_time"] = $map["end_time"];
            }
            if( !empty($map["order_sn"]) ){
                $url.="&order_sn=".$map["order_sn"];
                $Arr["order_sn"] = $map["order_sn"];
            }
            $this->assign("Arr",$Arr);
            $this->assign("url",$url);
        }

        return $this->fetch();
    }


    public function edit(){
        $id = input('param.');

        $data = Db::name('up_goods')
            ->alias("po")
            ->join("goods g","g.id=po.goods_id")
            //->join("address a","a.id=po.address_id")
            ->join("user u","u.id=po.user_id")
            ->where("po.id=".$id['id'])
            ->field("u.nickname,u.account,g.name,po.*")
            ->find();
        //$addr = str_replace('>',' ',$data['details']);

        if(request()->isAjax()){
            $d = input('post.');
            //print_r($d);exit;
            $d['f_time'] = time();
            $d['status'] = 3;

            $dd = input('post.id');

            $result = Db::name('up_goods')->where('id',$dd)->update($d);
            if($result){
                $arr = [
                    'result' => 0,
                    'msg' => '发货成功',
                ];
            }else{
                $arr = [
                    'result' => 1007,
                    'msg' => '发货失败',
                ];
            }
            $this->json_return($arr);
        }

        $this->assign('data',$data);

        //$this->assign('addr',$addr);
        return $this->fetch();
    }

    public function detail(){
        $id = input('param.');

        $data = Db::name('up_goods')
            ->alias("po")
            ->join("goods g","g.id=po.goods_id")
            //->join("address a","a.id=po.address_id")
            ->join("user u","u.id=po.user_id")
            ->where("po.id=".$id['id'])
            ->field("u.nickname,u.account,g.name,po.*")
            ->find();
        //$addr = str_replace('>',' ',$data['details']);

        $this->assign('data',$data);

        //$this->assign('addr',$addr);
        return $this->fetch();
    }
}